const router = require('koa-router')()

const albums = require("../public/data/dachang.json")
const { execQuery } = require("../db/query")

router.prefix('/interview')

/* 获取大厂专辑列表 */
router.get('/album/0', function (ctx, next) {
  ctx.body = albums
})

/* 获取专辑详情 */
router.get('/album/:aid', async function (ctx, next) {
  console.log("aid=", ctx.params.aid);

  try {
    const { query } = albums.find(item => item.albumid == ctx.params.aid)
    console.log("query=", query);

    const results = await execQuery(query)

    /* 拼接正确答案 */
    for (let i = 0; i < results.length; i++) {
      const question = results[i]

      const ros = await execQuery(
        `select * from roption WHERE qid = ${question.id}`
      )
      question.ros = ros

      const wos = await execQuery(
        `select * from woption WHERE qid = ${question.id}`
      )
      question.wos = wos
      // console.log("universal question=",question);
    }

    console.log("universal results=",results);
    ctx.body = results

  } catch (error) {
    ctx.body = error
  }
})

/* 获取问题列表的选项（出题） */
router.post('/options', async function (ctx, next) {
  console.log("body=", ctx.request.body);
  ctx.body = ctx.request.body;
})

module.exports = router
